var mongoose = require('mongoose')
var Art = mongoose.model('Art')
var co = require('co')
var Category = mongoose.model('Category')
var koa_request = require('koa-request')
var Promise = require('bluebird')
var request = Promise.promisify(require('request'))
var _lodash = require('lodash')
// index page
exports.findAll = function*() {
  var categories = yield Category
    .find({})
    .populate({
      path: 'articles',
      select: 'title poster',
      options: { limit: 6 }
    })
    .exec()
    return categories
}

// search page
exports.searchByCategoty = function *(catName) {
  var categories = yield Category
      .find({name: catName})
      .populate({
        path: 'articles',
        select: 'title poster abstract isSend'
      })
      .exec()
      return categories
}
exports.searchByName = function *(q) {
  var arts = yield Art
      .find({title: new RegExp(q + '.*', 'i')})
      .exec()
  return arts
}

exports.searchById = function *(id) {
  var arts = yield Art
      .findOne({_id: id})
      .exec()
  return arts
}